package com.yanqu.road.dao.impl.activity.hsisland;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.hsisland.HsIslandPosition;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandIslandData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;

public class HsIslandIslandDataDaoImpl extends TempDataObjectDao<HsIslandIslandData> {

    public static String getHsIslandIslandDataSql = "select * from `t_c_activity_hs_island_island_data` where `activityId`=? and `groupId`=? and `unionId`=? and `floorId`=?";
    public Map<Integer, HsIslandIslandData> getHsIslandIslandMap(int activityId,int groupId,String unionId,int floorId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            Map<Integer, HsIslandIslandData> map = new LinkedHashMap<>();
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getHsIslandIslandDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            call.setString(i++, unionId);
            call.setInt(i++, floorId);
            rs = call.executeQuery();
            while (rs.next()) {
                HsIslandIslandData data = parse(rs);
                map.put(data.getIslandId(), data);
            }
            return map;
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
    }

    public static String updateHsIslandIslandDataSql = "update `t_c_activity_hs_island_island_data` set keyNum=?,fourLaPositions=?,lightPositionList=?,keyShowRate=?,keyMustShowRate=?,allEventNum=?,allLaNum=?,delFlag=?,createTime=? where activityId=? and groupId=? and unionId=? and floorId=? and islandId=?";
    @Override
    protected boolean updateData(HsIslandIslandData hsIslandIslandData) {
        hsIslandIslandData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getKeyNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandIslandData.getFourLaPositions(), SerializerFeature.DisableCircularReferenceDetect)));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandIslandData.getLightPositionList())));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getKeyShowRate()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getKeyMustShowRate()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getAllEventNum()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getAllLaNum()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getDelFlag()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandIslandData.getCreateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hsIslandIslandData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getFloorId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getIslandId()));
        if (getDb().executeNoneQuery(updateHsIslandIslandDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHsIslandIslandDataSql = "insert into `t_c_activity_hs_island_island_data`(activityId,groupId,unionId,floorId,islandId,keyNum,fourLaPositions,lightPositionList,keyShowRate,keyMustShowRate,allEventNum,allLaNum,delFlag,createTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(HsIslandIslandData hsIslandIslandData) {
        hsIslandIslandData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hsIslandIslandData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getFloorId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getIslandId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getKeyNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandIslandData.getFourLaPositions(), SerializerFeature.DisableCircularReferenceDetect)));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandIslandData.getLightPositionList())));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getKeyShowRate()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getKeyMustShowRate()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getAllEventNum()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getAllLaNum()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandIslandData.getDelFlag()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandIslandData.getCreateTime()));
        if (getDb().executeNoneQuery(insertHsIslandIslandDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HsIslandIslandData parseData(ResultSet rs) throws SQLException {
        HsIslandIslandData hsIslandIslandData = new HsIslandIslandData();
        hsIslandIslandData.setActivityId(rs.getInt("activityId"));
        hsIslandIslandData.setGroupId(rs.getInt("groupId"));
        hsIslandIslandData.setUnionId(rs.getString("unionId"));
        hsIslandIslandData.setFloorId(rs.getInt("floorId"));
        hsIslandIslandData.setIslandId(rs.getInt("islandId"));
        hsIslandIslandData.setKeyNum(rs.getInt("keyNum"));
        if (!StringUtils.isNullOrEmpty(rs.getString("fourLaPositions"))) {
            hsIslandIslandData.setFourLaPositions(JSON.parseObject(rs.getString("fourLaPositions"), new TypeReference<Map<String, List<HsIslandPosition>>>() {}));
        }
        hsIslandIslandData.setDelFlag(rs.getInt("delFlag"));
        hsIslandIslandData.setCreateTime(rs.getLong("createTime"));
        if (!StringUtils.isNullOrEmpty(rs.getString("lightPositionList"))) {
            hsIslandIslandData.setLightPositionList(JSON.parseObject(rs.getString("lightPositionList"), new TypeReference<List<Set<String>>>() {}));
        }
        hsIslandIslandData.setKeyShowRate(rs.getInt("keyShowRate"));
        hsIslandIslandData.setKeyMustShowRate(rs.getInt("keyMustShowRate"));
        hsIslandIslandData.setAllEventNum(rs.getInt("allEventNum"));
        hsIslandIslandData.setAllLaNum(rs.getInt("allLaNum"));
        return hsIslandIslandData;
    }
}
